/*
 * @Date: 2023-07-07 10:52:58
 * @LastEditors: lyx
 * @LastEditTime: 2024-01-04 15:49:11
 * @FilePath: \mt-middleware-server\src\main.ts
 * @Description:
 */
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { AppModule } from './app.module';
import { join } from 'path';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common';
import { HttpExceptionFilter } from './filters/http-exception/http-exception.filter';
import { TransformInterceptor } from './interceptors/transform/transform.interceptor';
import { FeishuExceptionsFilter } from './filters/feishu-exception/feishu-exception.filter';
import { AllExceptionFilter } from './filters/all-exception/all-exception.filter';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  // const app = await NestFactory.createMicroservice<NestExpressApplication>(AppModule, {
  //   transport: Transport.KAFKA,
  //   options: {
  //     client: {
  //       brokers: ['localhost:9092'],
  //     },
  //     consumer: {
  //       groupId: 'my-kafka-consumer',
  //     },
  //   },
  // });

  // 连接 kafka
  // app.connectMicroservice({
  //   transport: Transport.KAFKA,
  //   options: {
  //     client: {
  //       // clientId: config.get('kafka.clientId'),
  //       brokers: ['k8sm1.supcon5t.com:31092'],
  //       // retry: { retries: config.get('kafka.retryCount') },
  //     },
  //     consumer: { groupId: 'my-kafka-consumer' },
  //   },
  // });
  // app.startAllMicroservices();

  // 使用自定义logger
  // app.useLogger(app.get(WINSTON_LOGGER_TOKEN));
  // 静态资源
  app.useStaticAssets(join(__dirname, '..', 'public'), { prefix: '/static' });
  // 管道验证
  app.useGlobalPipes(new ValidationPipe());
  // 全局注册错误的过滤器
  app.useGlobalFilters(
    new AllExceptionFilter(app.get(WINSTON_MODULE_PROVIDER) as Logger),
    new FeishuExceptionsFilter(app.get(WINSTON_MODULE_PROVIDER) as Logger),
    new HttpExceptionFilter(app.get(WINSTON_MODULE_PROVIDER) as Logger),
  );
  // 全局注册拦截器
  app.useGlobalInterceptors(new TransformInterceptor());
  // Swagger 文档
  const config = new DocumentBuilder()
    .setTitle(process.env.swagger_title)
    .setDescription(process.env.swagger_description)
    .setVersion(process.env.swagger_version)
    .addTag(process.env.swagger_tag)
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api-docs', app, document);
  const PORT = process.env.nest_port || 3000;
  await app.listen(PORT);
  console.log(`项目地址: http://localhost:${PORT}`);
  console.log(`项目Swagger地址: http://localhost:${PORT}/api-docs`);
}
bootstrap();
